package net.ewon.avatar.common.util;

import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import net.ewon.avatar.common.pkg.AvatarPackageFile;

public class AvatarPackageFileVisitor extends SimpleFileVisitor<Path> {

	private List<AvatarPackageFile> avatarPackageFiles = new ArrayList<AvatarPackageFile>();

	private Path packageLocation;

	public AvatarPackageFileVisitor(Path packageLocation) {
		this.packageLocation = packageLocation;
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
			throws IOException {
		// We ignore the file starts with a dot. Don't use
		// file.getFileName().startsWith. see Javadoc for why.
		if (!file.getFileName().toString().startsWith(".")) {
			Path relativePath = packageLocation.relativize(file);

			AvatarPackageFile avatarPackageFile = new AvatarPackageFile(
					relativePath.toString(), Files.readAllBytes(file));
			avatarPackageFiles.add(avatarPackageFile);
		}

		return CONTINUE;
	}

	public List<AvatarPackageFile> getAvatarPackageFiles() {
		return avatarPackageFiles;
	}
}
